# Contributor: Mark Pashmfouroush <mark@markpash.me>
# Contributor: Francesco Colista <fcolista@alpinelinux.org>
# Maintainer: Mark Pashmfouroush <mark@markpash.me>
pkgname=coredns
pkgver=1.9.3
pkgrel=0
pkgdesc="fast and flexible DNS server"
url="https://github.com/coredns/coredns"
license="Apache-2.0"
arch="all"
options="net chmod-clean"
makedepends="go libcap unbound-libs unbound-dev"
depends="unbound-libs"
install="$pkgname.pre-install"
subpackages="$pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/coredns/coredns/archive/v$pkgver.tar.gz
	coredns.confd
	coredns.initd
	coredns.logrotated
	ensure-cgo.patch
	plugin.cfg.enabled
	"

# secfixes:
#   1.9.3-r0:
#     - CVE-2022-27191
#     - CVE-2022-28948

export GOPATH="$srcdir"

prepare() {
	default_prepare
	cp $srcdir/plugin.cfg.enabled $builddir/plugin.cfg
}

build() {
	CGO_ENABLED=1 make
}

check() {
	cd "$builddir"/request; go test ./...
	cd "$builddir"/core; go test ./...
	cd "$builddir"/coremain; go test ./...
	cd "$builddir"/plugin; go test ./...
}

package() {
	install -Dm755 coredns "$pkgdir"/usr/bin/coredns
	setcap cap_net_bind_service=+ep "$pkgdir"/usr/bin/coredns

	install -Dm755 "$srcdir"/coredns.initd "$pkgdir"/etc/init.d/coredns
	install -Dm644 "$srcdir"/coredns.confd "$pkgdir"/etc/conf.d/coredns
	install -Dm 644 "$srcdir"/coredns.logrotated "$pkgdir"/etc/logrotate.d/coredns

	install -d "$pkgdir"/etc/coredns
	install -d "$pkgdir"/var/log/coredns
}

cleanup_srcdir() {
	export GOPATH="$srcdir"
	go clean -modcache
	default_cleanup_srcdir
}

sha512sums="
d0c24c5d9f27883519e0d6edf917c6e0051b80627a320a20f6d5ee3e154f65790d1f66b5e0255aeed66874ff5826b2ab3d1237d6fd53452e7dfaf3b34b17fdfb  coredns-1.9.3.tar.gz
90300a3035b00b58f362b2c12dfaeee21b889e10e90600523b5785c907dfbd7e515c3269dd29a9d4c758990e78ede8343edfac0bc022bd3a23652543ba2d42d3  coredns.confd
06d9fcf227e064f8ea21f4f003d33611aabf2d75b2e6e097dc10af1db42ea823d15c26649e52584da4fb4a85e87b3f27959a44ce873f176c2a082624f6845f38  coredns.initd
c697c08c3b1153ae224a1eeb3521f9ee594de4852bc6ef78d94dc7f6f680517659a653ab5c13ea03918deb79e6654ef11512ba4c90fe06ea4591fefbab876ece  coredns.logrotated
9f2c08750b42f0901bc190a4c829a5a003e4c3aa9bd55e9a2f37f68f446cdd15b60227b7d2381d85b49674580f7c0206af76345d6dd5160401772c3efbf50ab6  ensure-cgo.patch
6746632085004303d8c21628bcc34e98025a523ee0c4a0db589bae9d9c9ea1b403d84ee70dd7217154956be67d25344560319c8c3ac919370f0027c3092687df  plugin.cfg.enabled
"
